package cn.ncupyh;

import java.util.Scanner;
public class RomNumber {

    public static void main(String[] args) {
        //将数字字符(<=9)转化为罗马数字:Ⅰ   Ⅱ   Ⅲ    Ⅳ    Ⅴ    Ⅵ    Ⅶ    Ⅷ    Ⅸ    Ⅹ
        //只能是数字
//        注意:罗马数字,没有0;

        //1.键盘录入:
        Scanner s = new Scanner(System.in);
        String is = null;
        while (true) {
            System.out.println("请输入只含数字的字符串:");
            is = s.next();
            boolean len =IfLength(is);
            boolean num = IfNumber(is);
            StringBuilder sb = new StringBuilder();
            //当长度和内容都符合标准时,进行下一步
            if(len && num){
                //替换罗马数字
                for (int i = 0;i<is.length();i++) {
                    int number = is.charAt(i)-48;
                    sb.append(RomNumber1(number));
                }
                System.out.println(sb.toString());
            } else{
                System.out.println("输入有误,请重新输入!");
            }
        }
        }

    //替换罗马数字1.
    public static String RomNumber1(int num){
        String[] arr = {" ","Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ"};
        return arr[num];
    }
//    //替换罗马数字2.
    //JDK12-->
//    public static String RomNumber2(char num){
////        String[] arr = {" ","Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ"};
//        String str = switch(num){
//            case '0' :str = "";
//            case '1' : str = "Ⅰ";
//            case '2' : str = "Ⅱ";
//            case '3' : str = "Ⅲ";
//            case '4' : str = "Ⅳ";
//            case '5' : str = "Ⅴ";
//            case '6' : str = "Ⅵ";
//            case '7' : str = "Ⅷ";
//            case '8' : str = "Ⅸ";
//            case '9' : str = "Ⅹ";
//            default :str = "error";
//        };
//        return str;
//    }
    //判断输入的字符串是否为数字
    public static boolean IfNumber(String is){
        for (int i = 0; i < is.length(); i++) {
            char ic = is.charAt(i);
            if(ic < '0' || ic > '9'){
                return false;
            }
        }
        return true;
    }
    //判断长度是否<=9
    public static boolean IfLength(String is){
        if(is.length() >9){
            return false;
        }
        return true;
    }

}
